package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utilis.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private EmpService empService;
    /**
     * 登录
     * */
    @RequestMapping("/login")
    public Result login(@RequestBody Emp emp){
        //1、接收请求参数
        log.info("登录请求参数：{}", emp);

        //2、调用业务层功能方法
        Emp loginEmp =empService.checkLogin(emp);

        //3、封装响应结果
        Map<String, Object> map = new HashMap<>();
        map.put("id", loginEmp.getId());
        map.put("username", loginEmp.getUsername());
        map.put("name", loginEmp.getName());

        //4、登陆成功颁发令牌
        String jwtToken = JwtUtils.generateJwt(map);
        map.put("token", jwtToken);

        //5、打印日志
        log.info("登录响应结果：{}", map);

        //6、响应结果
        return Result.success(map);
    }
}
